package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/*
 *@Description: 3、	年终奖上报（家家悦）流程：当填报节点，提交成功，
 * ①根据年份、月份(推送)、人事范围编码，更新uf_nzjcfhz中的BMSBYJJ、BMSBJTJ，
 * 将流程的requestid更新到uf_nzjcfhz中的LCID，并将zt更新为1；
 * ②根据流程明细中的年份、月份、人员编码，
 * 去更新uf_nzjcfjjy中的YJJ、JTJ；
 *@Author: Dusongsong
 *@Date: 2019/12/24
 */
public class WeaHRnzjJJYjxAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用: WeaHRnzjJJYjxAction");
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String STATE = "1";
        String message = "";

        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("查询主表SQL：" + "select * from " + tablename + " where requestid=" + requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));//外键
        String GJAHR = Util.null2String(rs.getString("GJAHR"));//年份
        String BMBM = Util.null2String(rs.getString("BMBM"));//部门编码
        String WERKS = Util.null2String(rs.getString("WERKS"));//人事范围编码
        if ("8004".equals(WERKS) || "8007".equals(WERKS) || "ZB01".equals(WERKS)) {
            //更新汇总表
            String SQL = "select bmsbyjj, bmsbjtj  from " + tablename + " where requestid='"+requestid+"'and  werks='" + WERKS + "' and gjahr='" + GJAHR + "' and bmbm = '"+BMBM+"'";
            baseBean.writeLog(SQL);
            rs.execute(SQL);
            rs.next();
            String BMSBYJJ = Util.null2String(rs.getString("BMSBYJJ"));//部门上报业绩奖
            String BMSBJTJ = Util.null2String(rs.getString("BMSBJTJ"));//部门上报集体奖
            String sql = "update uf_nzjcfhz set bmsbyjj='" + BMSBYJJ + "',bmsbjtj='" + BMSBJTJ + "',zt='" + STATE + "',lcid='" + requestid
                    + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and bmbm='" + BMBM + "' ";
            rs.executeUpdate(sql);
            baseBean.writeLog(sql);
            //更新明细表
            rs2.execute("select * from " + tablename + "_dt1 where mainid='" + mainid + "' and  werks = '"+WERKS+"' and gjahr = '"+GJAHR+"' and bmbm = '"+BMBM+"'");
            while (rs2.next()){
                String PERNR = Util.null2String(rs2.getString("PERNR"));//人员编号
                String YJJ = Util.null2String(rs2.getString("YJJ"));//业绩奖
                String JTJ = Util.null2String(rs2.getString("JTJ"));//集体奖
                String updateSQL = "update uf_nzjcfjjy set yjj='" + YJJ + "',jtj='" + JTJ + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and bmbm='" + BMBM + "' and pernr='"+PERNR+"'";
                rs2.executeUpdate(updateSQL);
                baseBean.writeLog(updateSQL);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(message);

            }
            return Action.SUCCESS;
        }else {
            //更新汇总表
            String SQL = "select bmsbyjj, bmsbjtj  from " + tablename + " where requestid='"+requestid+"'and  werks='" + WERKS + "' and gjahr='" + GJAHR + "'";
            baseBean.writeLog(SQL);
            rs.execute(SQL);
            rs.next();
            String BMSBYJJ = Util.null2String(rs.getString("BMSBYJJ"));//部门上报业绩奖
            String BMSBJTJ = Util.null2String(rs.getString("BMSBJTJ"));//部门上报集体奖
            String sql = "update uf_nzjcfhz set bmsbyjj='" + BMSBYJJ + "',bmsbjtj='" + BMSBJTJ + "',zt='" + STATE + "',lcid='" + requestid
                    + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "'";
            rs.executeUpdate(sql);
            baseBean.writeLog(sql);
            //更新明细表
            rs2.execute("select yjj,jtj,pernr from " + tablename + "_dt1 where mainid='" + mainid + "' and  werks = '"+WERKS+"' and gjahr = '"+GJAHR+"'");
            baseBean.writeLog("select yjj,jtj,pernr from " + tablename + "_dt1 where mainid='" + mainid + "' and  werks = '"+WERKS+"' and gjahr = '"+GJAHR+"'");
            while (rs2.next()){
                String PERNR = Util.null2String(rs2.getString("PERNR"));//人员编号
                String YJJ = Util.null2String(rs2.getString("YJJ"));//业绩奖
                String JTJ = Util.null2String(rs2.getString("JTJ"));//集体奖
                String updateSQL = "update uf_nzjcfjjy set yjj='" + YJJ + "',jtj='" + JTJ + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and pernr='"+PERNR+"'";
                rs2.executeUpdate(updateSQL);
                baseBean.writeLog(updateSQL);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(message);

            }
            return Action.SUCCESS;
        }

    }
}

